using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Threading;
using System.Diagnostics;
using System.Collections;

using System.Runtime.Serialization;
using System.Runtime.Versioning;
using System.Net.Sockets;
using System.ComponentModel;
using System.Resources;
using System.Reflection;
using Mud.EmlenMudMain;
//using Utils;

namespace BrainTechLLC
{
    [Serializable]
    public class AreaList
    {
        public AreaList() { }
        private List<string> rgAreaFNames = new List<string>();

        public List<string> AreaFileNames
        {
            get { return rgAreaFNames; }
            set { rgAreaFNames = value; }
        }

        public bool AddAreaToList(AreaData area)
        {
            if (rgAreaFNames.Contains(area.AreaFileName) == true)
            {
                // Area file name already exists!
                return false;
            }
            else
            {
                rgAreaFNames.Add(area.AreaFileName);
                return true;
            }
        }

        public static string LoadAreaList(int inVersion, ref bool fOldFormat)
        {
            EmlenMud.HostPathnameToMudDirectory = "C:\\shared\\EmlenMud\\BrainTechLLC.Silverlight.ClientApplication.Web";
            if (Directory.Exists(EmlenMud.HostPathnameToMudDirectory + "\\" + MiscConstants.DataFileDirectory) == false)
            {
                try { Directory.CreateDirectory(EmlenMud.HostPathnameToMudDirectory + "\\" + MiscConstants.DataFileDirectory); }
                catch (Exception ex) { EmlenMud.LogError("Could not create " + EmlenMud.HostPathnameToMudDirectory + "\\" + MiscConstants.DataFileDirectory, ex, LogType.Always); }
            }
            if (Directory.Exists(EmlenMud.HostPathnameToMudDirectory + "\\" + MiscConstants.AREA_DIR) == false)
            {
                try { Directory.CreateDirectory(EmlenMud.HostPathnameToMudDirectory + "\\" + MiscConstants.AREA_DIR); }
                catch (Exception ex) { EmlenMud.LogError("Could not create " + EmlenMud.HostPathnameToMudDirectory + "\\" + MiscConstants.AREA_DIR, ex, LogType.Always); }
            }
            if (Directory.Exists(EmlenMud.HostPathnameToMudDirectory + "\\" + MiscConstants.PLAYER_DIR) == false)
            {
                try { Directory.CreateDirectory(EmlenMud.HostPathnameToMudDirectory + "\\" + MiscConstants.PLAYER_DIR); }
                catch (Exception ex) { EmlenMud.LogError("Could not create " + EmlenMud.HostPathnameToMudDirectory + "\\" + MiscConstants.PLAYER_DIR, ex, LogType.Always); }
            }
            EmlenMud.WorldTemplates.arealist.AreaFileNames.Clear();
            bool fUseOldFileFormats = (MiscConstants.DataStorageFormat == DataStorageFormatTypes.OldTextFileFormat);

            fOldFormat = true;
            string sz = EmlenMud.WorldTemplates.arealist.LoadOldStyleAreaList();

            return EmlenMud.HostPathnameToMudDirectory + "\\" + MiscConstants.AREA_DIR + "\\";
        }

        public string FindAreaListFile()
        {
            string szAreaList = EmlenMud.HostPathnameToMudDirectory + "\\" + MiscConstants.DataFileDirectory + "\\";
            if (File.Exists(szAreaList + MiscConstants.AREA_LIST) == false)
            {
                szAreaList = EmlenMud.HostPathnameToMudDirectory + "\\" + MiscConstants.AREA_DIR + "\\";
                if (File.Exists(szAreaList + MiscConstants.AREA_LIST) == false)
                {
                    string[] rg = EmlenMud.HostPathnameToMudDirectory.Split('\\');
                    string szNewStartupPath = "";
                    for (int i = 0; i < rg.Length - 1; i++)
                        szNewStartupPath += rg[i] + "\\";
                    szAreaList = szNewStartupPath + "\\" + MiscConstants.DataFileDirectory + "\\";
                    if (File.Exists(szAreaList + MiscConstants.AREA_LIST) == false)
                    {
                        szAreaList = szNewStartupPath;
                        if (File.Exists(szAreaList + MiscConstants.AREA_LIST) == false)
                        {
                            return null;
                        }
                    }
                }
            }
            return szAreaList;
        }

        public string LoadOldStyleAreaList()
        {
            string szAreaList = FindAreaListFile();
            if (szAreaList == null) { return "!Unable to locate area list.  Aborting."; }

            FileStream fs = null;

            try { fs = new FileStream(szAreaList + MiscConstants.AREA_LIST, FileMode.Open, FileAccess.Read); }
            catch (IOException ex) { return "!Error reading area list: " + ex.Message; }

            StreamReader sr = new StreamReader(fs);
            string szArea = "";
            while (szArea != "DONE" && sr.EndOfStream == false)
            {
                szArea = sr.ReadLine().Trim();
                rgAreaFNames.Add(szArea);
            }
            sr.Close();
            return szAreaList;
        }
    }
}
//namespace Mud.EmlenMudMain
//{
//  [Serializable]
//  public class AreaList : MudThing, IAreaList
//  {
//    public AreaList() : base(EmlenMud) { }
//    private ArrayList rgAreaFNames = new ArrayList(16);

//    public ArrayList AreaFileNames
//    {
//      get { return rgAreaFNames; }
//      set { rgAreaFNames = value; }
//    }

//    public static bool SaveAreaList(int outVersion)
//    {
//      string szFName = EmlenMud.HostPathnameToMudDirectory + "\\" + MiscConstants.DataFileDirectory + "\\" + typeof(AreaList).Name.ToString() + outVersion.ToString();
//      return SerializationHelper.SerializeObjectOut(EmlenMud.WorldTemplates.arealist, szFName, true);
//    }

//    public bool AddAreaToList(AreaData area)
//    {
//      if (EmlenMud == null) AssignEmlenMud(EmlenMud);
//      if (rgAreaFNames.Contains(area.AreaFileName) == true)
//      {
//        // Area file name already exists!
//        return false;
//      }
//      else
//      {
//        rgAreaFNames.Add(area.AreaFileName);
//        AreaList.SaveAreaList(1);
//        return true;
//      }
//    }

//    public static bool SaveAllAreas(int outVersion)
//    {
//      EmlenMud.WorldTemplates.arealist.AreaFileNames.Clear();
//      for (int i = 0; i < EmlenMud.WorldTemplates.AllAreas.Count; i++)
//      {
//        AreaData area = (AreaData)EmlenMud.WorldTemplates.AllAreas[i];
//        EmlenMud.WorldTemplates.arealist.AreaFileNames.Add(area.AreaFileName);
//      }
//      SaveAreaList(outVersion);
//      for (int i = 0; i < EmlenMud.WorldTemplates.AllAreas.Count; i++)
//      {
//        AreaData area = (AreaData)EmlenMud.WorldTemplates.AllAreas[i];
//        if (EmlenMud.WorldTemplates.SerializeAreaOut(area, outVersion) == false)
//        {
//          EmlenMud.LogError("Could not write out area " + area.AreaFileName + ".", null, LogType.Always);
//        }
//      }
//      return true;
//    }

//    public static string LoadAreaList(int inVersion, ref bool fOldFormat)
//    {
//      if (Directory.Exists(EmlenMud.HostPathnameToMudDirectory + "\\" + MiscConstants.DataFileDirectory) == false)
//      {
//        try { Directory.CreateDirectory(EmlenMud.HostPathnameToMudDirectory + "\\" + MiscConstants.DataFileDirectory); }
//        catch (Exception ex) { EmlenMud.LogError("Could not create " + EmlenMud.HostPathnameToMudDirectory + "\\" + MiscConstants.DataFileDirectory, ex, LogType.Always); }
//      }
//      if (Directory.Exists(EmlenMud.HostPathnameToMudDirectory + "\\" + MiscConstants.AREA_DIR) == false)
//      {
//        try { Directory.CreateDirectory(EmlenMud.HostPathnameToMudDirectory + "\\" + MiscConstants.AREA_DIR); }
//        catch (Exception ex) { EmlenMud.LogError("Could not create " + EmlenMud.HostPathnameToMudDirectory + "\\" + MiscConstants.AREA_DIR, ex, LogType.Always); }
//      }
//      if (Directory.Exists(EmlenMud.HostPathnameToMudDirectory + "\\" + MiscConstants.PLAYER_DIR) == false)
//      {
//        try { Directory.CreateDirectory(EmlenMud.HostPathnameToMudDirectory + "\\" + MiscConstants.PLAYER_DIR); }
//        catch (Exception ex) { EmlenMud.LogError("Could not create " + EmlenMud.HostPathnameToMudDirectory + "\\" + MiscConstants.PLAYER_DIR, ex, LogType.Always); }
//      }
//      EmlenMud.WorldTemplates.arealist.AreaFileNames.Clear();
//      bool fUseOldFileFormats = (MiscConstants.DataStorageFormat == DataStorageFormatTypes.OldTextFileFormat);

//      if (fUseOldFileFormats == false)
//      {
//        if (SerializeIn(inVersion) == false)
//        {
//          // Serialization failed, revert to old area list format
//          fUseOldFileFormats = true;
//        }
//      }

//      if (fUseOldFileFormats)
//      {
//        fOldFormat = true;
//        string sz = EmlenMud.WorldTemplates.arealist.LoadOldStyleAreaList(EmlenMud);

//        if (MiscConstants.DataStorageFormat == DataStorageFormatTypes.CompressedSerialization && 
//          sz[0] != '!')
//        {
//          AreaList.SerializeOut(1);
//          return sz;
//        }       
//      }
//      return EmlenMud.HostPathnameToMudDirectory + "\\" + MiscConstants.AREA_DIR + "\\";
//    }

//    public static bool SerializeIn(int inVersion)
//    {
//      string szFName = EmlenMud.HostPathnameToMudDirectory + "\\" + MiscConstants.DataFileDirectory + "\\" + typeof(AreaList).Name.ToString() + inVersion.ToString();
//      Object o = SerializationHelper.SerializeObjectIn(szFName);
//      if (o == null) return false;
//      if (o is AreaList)
//      {
//        AreaList alist = (AreaList)o;
//        alist.AssignEmlenMud(EmlenMud);
//        EmlenMud.WorldTemplates.arealist = alist;
//        return true;
//      }
//      return false;
//    }

//    public static bool SerializeOut(int outVersion)
//    {
//      string szFName = EmlenMud.HostPathnameToMudDirectory + "\\" + MiscConstants.DataFileDirectory + "\\" + typeof(AreaList).Name.ToString() + outVersion.ToString();
//      return SerializationHelper.SerializeObjectOut(EmlenMud.WorldTemplates.arealist, szFName, true);
//    }

//    public string FindAreaListFile()
//    {
//      string szAreaList = EmlenMud.HostPathnameToMudDirectory + "\\" + MiscConstants.DataFileDirectory + "\\";
//      if (File.Exists(szAreaList + MiscConstants.AREA_LIST) == false)
//      {
//        szAreaList = EmlenMud.HostPathnameToMudDirectory + "\\" + MiscConstants.AREA_DIR + "\\";
//        if (File.Exists(szAreaList + MiscConstants.AREA_LIST) == false)
//        {
//          string[] rg = EmlenMud.HostPathnameToMudDirectory.Split('\\');
//          string szNewStartupPath = "";
//          for (int i = 0; i < rg.Length - 1; i++)
//            szNewStartupPath += rg[i] + "\\";
//          szAreaList = szNewStartupPath + "\\" + MiscConstants.DataFileDirectory + "\\";
//          if (File.Exists(szAreaList + MiscConstants.AREA_LIST) == false)
//          {
//            szAreaList = szNewStartupPath;
//            if (File.Exists(szAreaList + MiscConstants.AREA_LIST) == false)
//            {
//              return null;
//            }
//          }
//        }
//      }
//      return szAreaList;
//    }

//    public string LoadOldStyleAreaList()
//    {
//      string szAreaList = FindAreaListFile(EmlenMud);
//      if (szAreaList == null) { return "!Unable to locate area list.  Aborting."; }

//      FileStream fs = null;

//      try { fs = new FileStream(szAreaList + MiscConstants.AREA_LIST, FileMode.Open, FileAccess.Read); }
//      catch (IOException ex) { return "!Error reading area list: " + ex.Message; }

//      StreamReader sr = new StreamReader(fs);
//      string szArea = "";
//      while (szArea != "DONE" && sr.EndOfStream == false)
//      {
//        szArea = sr.ReadLine().Trim();
//        rgAreaFNames.Add(szArea);
//      }
//      sr.Close();
//      return szAreaList;
//    }

//    public void LoadAllAreas()
//    {
//      if (EmlenMud == null) AssignEmlenMud(EmlenMud);
//      for (int i = 0; i < rgAreaFNames.Count; i++)
//      {
//        string szFileName = (string)rgAreaFNames[i];
//        AreaData area = (AreaData)EmlenMud.WorldTemplates.SerializeAreaIn(szFileName, 1);
//      }
//    }
//  }
//}